package gui;

import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
/**
 * GUI Klasse
 *
 * @author	Michael Wolf
 * @author	Tanja Goletz
 * @version	1.0
 */
@SuppressWarnings("serial")
public class NotenInput extends JTextField{

	String oldValue = "";
	NotenGUI gui;
	boolean isFehltage = false;
	
	
	public NotenInput(NotenGUI gui, final boolean isFehltage){
		this.gui = gui;
		this.isFehltage = isFehltage;
		
		AbstractDocument document = (AbstractDocument) getDocument();
		
		document.setDocumentFilter(new DocumentFilter(){
			@Override
			public void insertString(FilterBypass fb, int offset,
					String string, AttributeSet attr)
					throws BadLocationException {
				
					if(checkInput(string) && fb.getDocument().getLength() < (isFehltage ? 2:1)) {
						super.insertString(fb, offset, string, attr);
						JTextField nextTextField = NotenInput.this.gui.getNextTextField(NotenInput.this);
						if (nextTextField != null && fb.getDocument().getLength() >= (isFehltage ? 2:1)) nextTextField.requestFocus();
					}
				 
			}
			
			@Override
			public void replace(FilterBypass fb, int offset, int length,
					String text, AttributeSet attrs)
					throws BadLocationException {
				if(checkInput(text) && fb.getDocument().getLength() < (isFehltage ? 2:1)){
					super.replace(fb, offset, length, text, attrs);
					JTextField nextTextField = NotenInput.this.gui.getNextTextField(NotenInput.this);
					if (nextTextField != null && fb.getDocument().getLength() >= (isFehltage ? 2:1)) nextTextField.requestFocus();
				}
			}
		});
		
		setDocument(document);
	}
	
	private boolean checkInput(String input) {
		return input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4") || input.equals("5") || input.equals("6") || (isFehltage && input.equals("0"));
	}
		
}
